package org.jsola.hr.helper;

import org.jsola.common.SafeKit;
import org.jsola.hr.dto.NewsGroupAddDTO;
import org.jsola.hr.dto.NewsGroupUpdateDTO;
import org.jsola.hr.entity.NewsGroupDO;
import org.jsola.hr.query.NewsGroupQuery;
import org.jsola.hr.service.INewsGroupService;
import org.jsola.hr.vo.NewsGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * 官网新闻类型分组
 *
 * @author zhr
 */
@Component
public class NewsGroupTestHelper extends BaseTestHelper {
    
    @Autowired
    private INewsGroupService newsGroupService;

    /**
     * 生成add测试数据
     * @return NewsGroupAddDTO
     */
    public NewsGroupAddDTO getNewsGroupAddDTO(){
        NewsGroupAddDTO newsGroupAddDTO = new NewsGroupAddDTO();
        newsGroupAddDTO.setCode(getRandomValue("varchar",50));
        newsGroupAddDTO.setName(getRandomValue("varchar",50));
        newsGroupAddDTO.setType(SafeKit.getInteger(getRandomValue("tinyint",2)));
        return newsGroupAddDTO;
    }


    /**
     * 生成update测试数据
     * @return NewsGroupUpdateDTO
     */
    public NewsGroupUpdateDTO getNewsGroupUpdateDTO(NewsGroupVO newsGroupVO){
        return newsGroupService.selectDOById(newsGroupVO.getId(), SITE_ID).to(NewsGroupUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return NewsGroupQuery
     */
    public NewsGroupQuery getNewsGroupQuery() {
        NewsGroupQuery newsGroupQuery = new NewsGroupQuery();
        List<NewsGroupDO> newsGroupDOList = newsGroupService.list(new NewsGroupDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(newsGroupDOList)) {
            NewsGroupDO newsGroupDO = newsGroupDOList.get(0);
            newsGroupQuery = newsGroupDO.to(NewsGroupQuery.class);
        }
        return newsGroupQuery;
    }

    /**
     * 保存示例
     * @return NewsGroupVO
     */
    public NewsGroupVO saveNewsGroupExample(){
        NewsGroupAddDTO addDTO = this.getNewsGroupAddDTO();
        NewsGroupVO newsGroupVO = newsGroupService.save(addDTO,buildTokenUser());
        return newsGroupVO;
    }


}

